$(document).ready(function() {
	$("a[data-confirm]").on("click",function(e){ 
		e.preventDefault();
		var message = $(this).data("confirm");
		var href = $(this).attr("href");
		if(message == ''){ 
			message = '确定该操作?';
		}
		bootbox.confirm(message,function(result){ 
			if(!result){ 
				
			}else{ 
				location.href = href; 
			}
		})
	});
	$(document).on("change","input.select-all",function(){ 
		var checked = $(this).prop("checked");
		$("input.select-item").prop("checked",checked).trigger("change");
	});
	$(document).on("click",".ajax-load",function(e){ 
		e.preventDefault();
		var href =$(this).attr("href");
		var id = parseInt(Math.random()*100);
		if($("#ajax-modal"+id).length<1){ 
			$("body").append('<div id="ajax-modal'+id+'" class="modal fade" tabindex="-1" style="display: none;"></div>');
		}
		var $modal = $('#ajax-modal'+id);
		var loader = loading();
		 $modal.load(href, '', function(result){
		 	try{
		 		result = JSON.parse(result);
		 		if(result.status == 'failed'){
		 			loader.remove();
		 			return tips(result.message,'danger');
		 		}else{ 
		 			loader.remove();
		 			setTimeout(function(){ 
		 				if(result.hasOwnProperty("url") && result.url){ 
		 					location.href = result.url;
		 				}else{ 
		 					location.reload();
		 				}
		 			},1500);
		 			return tips(result.message,'success');
		 		}
		 		
		 	}catch(e){ 
		 	
		 	}
		 	loader.remove();
		 	if(!result){ 
		 		tips('加载失败','danger');
		 	}else{ 
		 		$modal.modal({backdrop: 'static', keyboard: false});
		 	}
		    
		});
		$('#ajax-modal'+id).on('hide.bs.modal', function () {
	    	setTimeout(function(){ 
	    		 $('#ajax-modal'+id).remove();
	    	},500);
	    	
	  })
	});
});
//提示信息
function tips(message, type) {
	if (typeof type == 'undefined') {
		type = 'success';
	}
	switch (type) {
		case 'danger':
			Notify(message, 'top-right', '3000', 'danger', 'fa-bolt', true);
			break;
		case 'warning':
			Notify(message, 'top-right', '3000', 'warning', 'fa-warning', true);
			break;
		case 'success':
			Notify(message, 'top-right', '3000', 'success', 'fa-check', true);
			break;
		default:
			Notify(message, 'top-right', '3000', 'success', 'fa-check', true);
	}
}

//loading
function loading(message){ 
	message || (message = '正在加载…');

    var html = '<div id="toast-loading" class="toast-container" style="left: 895px; top: 40%;"><div class="toast loading toast-loading"><div class="toast-message">'+message+'</div></div></div>';
    $("body").append(html);
    var parent = $("#toast-loading");    
    var left =($(window).width() - parent.width()) / 2;
    parent.css("left",left);
    parent.css("top","40%");
    return parent;
}
function showLoader(){ 	
		$(".loading-container").removeClass("loading-inactive");
}
function hideLoader(){ 
	$(".loading-container").addClass("loading-inactive");
}
